var server = {
	api: {
		'LOGIN'          : '/user/login',
		'SIGNIN'         : '/user/signin',
		
		'ADD_BLOG'       : '/blog/add',
		'GET_BLOG'       : '/blog/list',
		'GET_TAG'        : '/blog/tags',
		'ADD_TAG'        : '/blog/addtag',
		'GET_DETAILS'    : '/blog/details',

		'ADD_COLLECTS'   : '/collects/add',
		'GET_COLLECTS'   : '/collects/list',
	},

	getApi: function(name){
	    var host = '';

	    if (!this.api[name]) return host;
	    return host + this.api[name];
	},

	// ajax封装
	ajax: function(param){
		var self = this;
		var options = {
			dataType: "json", 
			timeout: 1000,
			error: function(){
				alert('请求数据出错');
			}
		}
		
		options = $.extend(options, param);

		options.success = function(res){
			if (res.code == '10001') {
				self.storage('user', 'null');
				location.href = "/admin/login";
				return false;
			} else if (res.code == '200') {
				param.success(res);
			} else {
				alert(res.message);
			}
		}

		if (!options.type) options.type = 'get';
		if (options.url) options.url = this.getApi(param.url);

		$.ajax(options);
	},

	storage: function(key, value){
		var storage = localStorage;
		if (value) {
			if (typeof value != 'string') value = JSON.stringify(value);
			storage.setItem(key, value);
		}
		if (value == 'null') storage.setItem(key, '');
		else {
			var item = storage.getItem(key); 
			if (item) return $.parseJSON(item);
			return '';
		}
	}
}
